package com.inspur.gs.sbssdl.sbwhgl.bpbjgl.bpbjgl.back.bpbjlktz.determinations;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.spi.action.determination.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.spi.action.determination.AbstractDetermination;
import com.inspur.edp.bef.api.action.determination.IDeterminationContext;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.bef.api.action.determination.IBeforeRetrieveDtmContext;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import com.inspur.edp.svc.document.storage.entity.GspDocOperatingModes;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;

public class bpbjlktzDtmAfterSave1MgrAction extends AbstractDetermination {
	public bpbjlktzDtmAfterSave1MgrAction(IDeterminationContext context, IChangeDetail change) {
		super(context,change);
	}
	@Override
	public  void execute() {
		IEntityData currData = getContext().getCurrentData();
		if (currData == null) {
			return;
		}

		String rootId = "sbqr-root";
		Resource resource = new ClassPathResource("");
		try {
			String rootPath = resource.getFile().getAbsolutePath();
			String pngPath = rootPath + File.separator + "web" + File.separator + "apps" + File.separator + "sbQRCode" + File.separator + rootId ;
			if (!new File(pngPath).exists()) new File(pngPath).mkdirs();
			if (currData.getValue("sbbh") != null && !StringUtils.isBlank(currData.getValue("sbbh").toString())) {

				String fileName = currData.getValue("sbbh").toString();
				QRUtil qrUtil = new QRUtil();
				String sbbhsPath = qrUtil.generateQRCode(fileName, pngPath, 300, 300);
				byte[] sbbhs = Files.readAllBytes(Paths.get(sbbhsPath));

				// 上传到服务器
				RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
				DocStorageService docStorage = rpcClassHolder.getRpcClass(DocStorageService.class);

				GspDocMetadata metadata = new GspDocMetadata(rootId);
				metadata.setFileName(fileName);
				metadata.setDocType("png");
				metadata.setDocSize(String.valueOf(sbbhs.length));

				// 上传
				String attachmentId = docStorage.uploadDoc(fileName, metadata, sbbhs, GspDocOperatingModes.Formal);
				// 修改参数
				HashMap<String, Object> map = new HashMap<>();
				map.put("AttachmentId", attachmentId);
				map.put("FileName", metadata.getFileName() + metadata.getDocType());
				map.put("FileSize", new BigDecimal(metadata.getDocSize()));
				map.put("FileCreateTime", new Date());
//				getData().setMultiUdtValue("qrcode", map);
			}
		} catch (IOException e) {
			System.out.println("IOException: " + e.getMessage());
			throw new RuntimeException(e);
		} catch (Exception e) {
			System.out.println("二维码建立异常Exception: " + e.getMessage());
			throw new RuntimeException(e);
		}


	}
	private IEntityData getData() {
		return (IEntityData)super.getContext().getCurrentData();
	}
}